package com.sk.sourcecircle.activity.mine.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.activity.home.activity.GroupDetailsActivity;
import com.sk.sourcecircle.adapter.GroupListAdapter;
import com.sk.sourcecircle.app.MyApplication;
import com.sk.sourcecircle.base.BaseActivity;
import com.sk.sourcecircle.callback.HandleDataCallBack;
import com.sk.sourcecircle.entity.GroupListBean;
import com.sk.sourcecircle.entity.HomeTabList;
import com.sk.sourcecircle.utils.AppApi;
import com.sk.sourcecircle.utils.MyUtils;
import com.sk.sourcecircle.utils.SnackUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by sk on 2017/11/23.
 */

public class MyCareActivity extends BaseActivity implements HandleDataCallBack, BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemChildClickListener {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;

    private int cateId;
    private List<GroupListBean.DataBean> allDataList = new ArrayList<>();
    private GroupListAdapter adapter;
    private boolean isLoadMore = false;
    private int page = 1;

    HomeTabList.DataBean dataBean;
    private LoadingDialog loadingDialog;

    private int postion;

    @Override
    protected int getContentView() {
        return R.layout.activity_my_care;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        loadingDialog = MyUtils.getLoadingDialog(mContext, false);

        dataBean = getIntent().getParcelableExtra("data");

        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new GroupListAdapter(R.layout.item_joined_group, allDataList);
        adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        dataRyc.setAdapter(adapter);
//        EasyLoadingMoreView loadMoreView = new EasyLoadingMoreView();
//        adapter.setLoadMoreView(loadMoreView);
        View emptyView = getLayoutInflater().inflate(R.layout.empty_view, null);
        adapter.setEmptyView(emptyView);
        adapter.setOnItemClickListener(this);
        adapter.setOnItemChildClickListener(this);
//        adapter.setOnLoadMoreListener(this, dataRyc);

        loadingDialog.show();
        AppApi.getJoinedGroup(String.valueOf(MyApplication.getInstance().getLat()), String.valueOf(MyApplication.getInstance().getLng()), this, 0, this);
    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        switch (tag) {
            case 0:
                GroupListBean groupListBean = JSON.parseObject(result, GroupListBean.class);
                List<GroupListBean.DataBean> dataBeanList = groupListBean.getData();
                allDataList.clear();
                allDataList.addAll(dataBeanList);
//                if (allDataList.size() < 10) {
//                    adapter.setEnableLoadMore(false);
//                } else {
//                    adapter.setEnableLoadMore(true);
//                }
//                if (isLoadMore) {
//                    adapter.loadMoreComplete();
//                    if (dataBeanList.size() == 0) {
//                        adapter.loadMoreEnd();
//                    }
//                    isLoadMore = false;
//                }
                adapter.notifyDataSetChanged();
                break;
            case 1:
                allDataList.get(postion).setFollow(1);
                adapter.notifyItemChanged(postion);
                SnackUtils.show(mRootView, "关注成功！");
                break;
            case 2:
                allDataList.get(postion).setFollow(0);
                adapter.notifyItemChanged(postion);
                SnackUtils.show(mRootView, "取关成功！");
                break;
        }

    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();
    }

//
//    @Override
//    public void onLoadMoreRequested() {
//        isLoadMore = true;
//        page++;
//        AppApi.getJoinedGroup(this, 0, MyCareActivity.this);
//    }


    @Override
    public void sure() {
        super.sure();
        AppApi.cancelCareGroup(allDataList.get(postion).getCommunity_id(), MyCareActivity.this, 2, MyCareActivity.this);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(mContext, GroupDetailsActivity.class);
        intent.putExtra("id", allDataList.get(position).getCommunity_id());
        startActivity(intent);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.item_bg:
                Intent intent = new Intent(mContext, GroupDetailsActivity.class);
                intent.putExtra("id", allDataList.get(position).getCommunity_id());
                startActivity(intent);
                break;
        }
    }
}
